package com.ruoyi.record.service.impl;

import java.util.Arrays;
import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.bean.BeanUtils;
import org.springframework.stereotype.Service;
import com.ruoyi.record.mapper.RecordAttendContentMapper;
import com.ruoyi.record.domain.RecordAttendContent;
import com.ruoyi.record.service.IRecordAttendContentService;
import com.ruoyi.common.core.text.Convert;
import org.springframework.transaction.annotation.Transactional;

/**
 * 考勤详情Service业务层处理
 * 
 * @author ruoyi
 * @date 2022-09-26
 */
@Service
public class RecordAttendContentServiceImpl extends ServiceImpl<RecordAttendContentMapper, RecordAttendContent> implements IRecordAttendContentService
{
    /**
     * 查询考勤详情
     * 
     * @param contentId 考勤详情主键
     * @return 考勤详情
     */
    @Override
    public RecordAttendContent selectRecordAttendContentByContentId(Long contentId)
    {
        return baseMapper.selectRecordAttendContentByContentId(contentId);
    }

    /**
     * 查询考勤详情列表
     * 
     * @param recordAttendContent 考勤详情
     * @return 考勤详情
     */
    @Override
    public List<RecordAttendContent> selectRecordAttendContentList(RecordAttendContent recordAttendContent)
    {
        return baseMapper.selectRecordAttendContentList(recordAttendContent);
    }

    /**
     * 新增考勤详情
     * 
     * @param recordAttendContent 考勤详情
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertRecordAttendContent(RecordAttendContent recordAttendContent)
    {
        for(Long userId : recordAttendContent.getUserIds()){
            RecordAttendContent attendContent = new RecordAttendContent();
            BeanUtils.copyBeanProp(attendContent, recordAttendContent);
            attendContent.setUserId(userId);
            recordAttendContent.setCreateTime(DateUtils.getNowDate());
            baseMapper.insert(attendContent);
        }
        return 1;
    }

    /**
     * 修改考勤详情
     * 
     * @param recordAttendContent 考勤详情
     * @return 结果
     */
    @Override
    public int updateRecordAttendContent(RecordAttendContent recordAttendContent)
    {
        recordAttendContent.setUpdateTime(DateUtils.getNowDate());
        return baseMapper.updateById(recordAttendContent);
    }

    /**
     * 批量删除考勤详情
     * 
     * @param contentIds 需要删除的考勤详情主键
     * @return 结果
     */
    @Override
    public int deleteRecordAttendContentByContentIds(String contentIds)
    {
        return baseMapper.deleteBatchIds(Arrays.asList(Convert.toLongArray(contentIds)));
    }

    /**
     * 删除考勤详情信息
     * 
     * @param contentId 考勤详情主键
     * @return 结果
     */
    @Override
    public int deleteRecordAttendContentByContentId(Long contentId)
    {
        return baseMapper.deleteById(contentId);
    }

    /**
     * 校验考勤
     *
     * @param recordAttendContent 考勤
     * @return 结果
     */
    @Override
    public boolean checkRecordAttendContent(RecordAttendContent recordAttendContent) {
        Long contentId = StringUtils.isNull(recordAttendContent.getContentId()) ? -1L : recordAttendContent.getContentId();
        LambdaQueryWrapper<RecordAttendContent> queryWrapper = new LambdaQueryWrapper<RecordAttendContent>().eq(RecordAttendContent::getAttendId, recordAttendContent.getAttendId());
        if(StringUtils.isNotNull(recordAttendContent.getUserIds())){
            queryWrapper.in(RecordAttendContent::getUserId, recordAttendContent.getUserIds());
        }else if(StringUtils.isNotNull(recordAttendContent.getUserId())){
            queryWrapper.eq(RecordAttendContent::getUserId, recordAttendContent.getUserId());
        }
        RecordAttendContent info = baseMapper.selectOne(queryWrapper.last("limit 1"));
        if (StringUtils.isNotNull(info) && info.getContentId().longValue() != contentId.longValue())
        {
            return false;
        }
        return true;
    }

    /**
     * 查询考勤详情列表
     *
     * @param recordAttendContent 考勤详情
     * @return 考勤详情
     */
    @Override
    public List<RecordAttendContent> selectRecordAttendContentReport(RecordAttendContent recordAttendContent)
    {
        return baseMapper.selectRecordAttendContentReport(recordAttendContent);
    }
}
